<?php
namespace app\admin\controller;
use think\Db;
use app\common\model\Group;
class User extends Base
{

    public function userList()
    {
        $userList = Db::name("user")
            ->alias("u")
            ->join("auth_group_user g","u.uid = g.uid")
            ->join("auth_group p","g.group_id = p.id")
            ->order('t_user.uid desc')
            ->paginate(10);
        $authGroup = Db::name("auth_group")->select();
        return view("/user-list",["userList" => $userList,"authGroup" => $authGroup]);
    }
    public function delete()
    {
        $userId = input("userId");
        $delete = Db::name("user")->where("uid",$userId)->delete();
        if($delete){
            return json(["code" => 1,"msg" => "删除成功"]);
        }
        return json(["code" => 0,"msg" => "删除失败"]);
    }

    public function updateRole()
    {
        $uid = input("uid");
        $groupId = input("groupId");
        if($uid =="" || $groupId == "undefined"){
            return json(["code" => 0 ,"msg" => "数据为空"]);
        }
        $update = Db::name("auth_group_user")->where("uid",$uid)->update(["group_id" => $groupId]);
        if($update){
            return json(["code" => 1 ,"msg" => "保存成功"]);
        }
        return json(["code" => 0 ,"msg" => "保存失败"]);
    }

    public function groupList()
    {
        $groupList = Group::all();
        $this->assign("groupList",$groupList);
        return $this->fetch("/group-list");
    }

    public function ruleDetail()
    {
        if (request()->isAjax()){
            //用户组拥有的权限
            $id = input("id");
            $ruleIdList = Db::name("auth_group_rule")->where("group_id",$id)->column("rule_id");
            return json($ruleIdList);
        }else{
            //列出所有权限
            $ruleIdList = Db::name("auth_rule")->column("id");
            $ruleList = array();
            foreach ($ruleIdList as $item){
                $ruleItem = Db::name("auth_rule")->field("name",true)->where("id",$item)->find();
                $ruleList[$ruleItem["pid"]][] = $ruleItem;
            }
            $this->assign("ruleList",$ruleList);
            return $this->fetch("/rule-detail");
        }
    }
    public function saveRule()
    {
        $groupId = input("groupId");
        $ruleIdList = input("post.ruleIdList/a");

        if ($groupId ==1){
            return json(["code" =>0 ,"msg" => "不能修改超级管理员权限"]);
        }
      
      $delGroup = Db::name('auth_group_rule')->where('group_id', $groupId)->delete();
      if($delGroup){
        return json(["code" =>0 ,"msg" => "权限修改失败了"]);
      }
        foreach($ruleIdList as $ruleId){
    
            $data["group_id"] = $groupId;
            $data["rule_id"] = $ruleId;
      
          $createRule =  Db::name("auth_group_rule")->insert($data);
          if(!$createRule){
             return json(["code" =>0 ,"msg" => "权限修改失败"]);
          }
               
        }
        return json(["code" =>1 ,"msg" => "权限修改成功"]);
    }
  
    public function delGroup()
    {
        $id = input("id");
        $delGroup = Db::name("auth_group")->where("id",$id)->delete();
        $delGroupRule = Db::name("auth_group_rule")->where("group_id",$id)->delete();//删除用户组对应权限
      
        if(!$delGroup){
            return json(["code" => 0 ,"msg" => "删除用户组失败"]);
        }
        return json(["code" => 1 ,"msg" => "删除用户组成功"]);
    }
  
    public function addGroup()
    {
        if(request()->isAjax()){
            $groupTitle = input("title");
            $groupId = Db::name("auth_group")->insertGetId(["title" => $groupTitle]);
            if($groupId){
                return json(["code" =>1,"msg" => "添加用户组成功","groupId" =>$groupId]);
            }
            return json(["code" =>0,"msg" => "添加用户组失败"]);
        }else{
            return $this->fetch("/group-del");
        }
    }
    public function logout()
    {
        session("uid",null);
        $this->success("退出登录成功","/admin/index/login");
    }
}